package com.longcai.controller.clyjuser;


import com.alibaba.fastjson.JSONObject;
import com.longcai.bean.ApiResult;
import com.longcai.bean.PageHelper;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.clyjuser.ClyjUserService;
import com.longcai.util.OSSUtils;
import io.swagger.annotations.ApiOperation;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.Map;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author M
 * @since 2019-03-31
 */
@RestController
@RequestMapping("/clyjUser")
public class ClyjUserController {

	
	private static org.slf4j.Logger logger = LoggerFactory.getLogger(ClyjUserController.class);
	
    @Autowired
    private ClyjUserService clyjUserService;

    /**
     * 获取会员列表
     *
     * @param clyjUser
     * @param pageHelper
     * @return
     */
    @ApiOperation(value = "会员列表")
    @GetMapping(value = "/clyjuserList")
    public PageTableResponse userList(ClyjUser clyjUser, PageHelper pageHelper) {

        return this.clyjUserService.userList(clyjUser, pageHelper);
    }

    /**
     * 根据id获取会员信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取会员信息")
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('clyj:user:query')")
    public ApiResult user(@PathVariable String id) {
    	if(StringUtils.isEmpty(id)){
    		logger.error("用户管理,查询用户信息，参数非法"
    				+"userId:"+id+"----------------"
    				);
    		return ApiResult.fail("用户管理,查询用户信息，参数非法");
    	}
    	ClyjUser user=	clyjUserService.selectUser(id);
        return ApiResult.success(user);
    }

    /**
     * 修改会员信息
     *
     * @param clyjUser
     * @return
     */
    @PostMapping(value = "/updateUser")
    public Integer updateUser(ClyjUser clyjUser, MultipartFile icon, MultipartFile CardFront, MultipartFile CardBack, MultipartFile CardHand) {
        ClyjUser u = this.clyjUserService.getName(clyjUser.getUsername());
        if (u != null) {
            throw new IllegalArgumentException(clyjUser.getUsername()+ "已存在");
        }
        if (icon != null) {
            //文档上传oss
            try {
                String fileName = icon.getOriginalFilename();
                String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
                InputStream inputStream = icon.getInputStream();
                Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/USER/");
                clyjUser.setPhoto(uploadByInputStream.get("path").toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (CardFront != null) {
            //文档上传oss
            try {
                String fileName = CardFront.getOriginalFilename();
                String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
                InputStream inputStream = CardFront.getInputStream();
                Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/USER/");
                clyjUser.setIdCardFront(uploadByInputStream.get("path").toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (CardBack != null) {
            //文档上传oss
            try {
                String fileName = CardBack.getOriginalFilename();
                String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
                InputStream inputStream = CardBack.getInputStream();
                Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/USER/");
                clyjUser.setIdCardBack(uploadByInputStream.get("path").toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (CardHand != null) {
            //文档上传oss
            try {
                String fileName = CardHand.getOriginalFilename();
                String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
                InputStream inputStream = CardHand.getInputStream();
                Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/USER/");
                clyjUser.setIdCardHand(uploadByInputStream.get("path").toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return this.clyjUserService.updateUser(clyjUser);
    }

    /**
     * 删除用户
     *
     * @param id
     */
    @PostMapping("{id}")
    public void delClyjUser(@PathVariable String id) {
        this.clyjUserService.delClyjUser(id);
    }
    
    /****
     * 更改用户开启状态
     */
    @RequestMapping("/changeUserState")
    public ApiResult<?>  changeUserState(@RequestBody JSONObject param){
    	String  userId=param.getString("param");
    	String openState=param.getString("openState");
    	if(StringUtils.isEmpty(userId)||StringUtils.isEmpty(openState)){
    		logger.error("更改用户开启状态失败,参数非法"+
    				"userId:"+userId+"--------------"+
    				"openState:"+openState+"--------------"
    				);
    		return ApiResult.fail("更改用户开启状态失败,参数非法");
    	}
    	int i=clyjUserService.changeUserState(userId,openState);
    	if(i<1){
    		return ApiResult.fail("更改用户开启状态失败");
    	}
    	return ApiResult.success();
    }
    
    

}

